package fr.insalyon.sims.broadcast.tools;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class DisplayPanel extends JPanel {
	// ------------------------------------------------------- CONSTANTS -- //
	
	private static final long serialVersionUID = 4179347592299137970L;

	// ------------------------------------------------------ ATTRIBUTES -- //
	
	private ImageBuilder builder;
	
	private Thread updateThread;
	
	private BufferedImage lastImage;
	
	// ---------------------------------------------------- CONSTRUCTORS -- //
	
	public DisplayPanel(ImageBuilder builder) {
		this.builder = builder;
	}
	
	// --------------------------------------------------------- METHODS -- //
	
	public void paintComponent(Graphics g) {
		if (lastImage != null)
			g.drawImage(lastImage, 0, 0, getWidth(), getHeight(), null);
	}
	
	public void start() {
		this.updateThread = new Thread() {
			public void run() {
				while (!isInterrupted()) {
					lastImage = builder.retrieveImage();
					System.out.println("image!");
					repaint();
				}
			}
		};
		this.updateThread.start();
	}
	
	public void stop() {
		this.updateThread.interrupt();
		this.updateThread = null;
	}
	
	// ------------------------------------------------- PRIVATE METHODS -- //
	// --------------------------------------------------- INNER CLASSES -- //
}
